$(function(){
  'use strict';

// preloader
  $(".preloader").fadeOut();

// sidebar
  $('.sidebar').sideNav({
    edge: 'right'
  });



// testimonial
  $(".testimonial").owlCarousel({
    items: 1,
    navigation: true,
    slideSpeed: 1000,
    dots: true,
    paginationSpeed: 400,
    singleItem: true,
    autoplay: true,
    loop: true
  });

// tabs
  $('ul.tabs').tabs();

// collapse
  $('.collapsible').collapsible();

});

function api_request(url, method, parm,formData){

  var apiurl    = 'http://order.dg1.cn:81/api.php/';    //域名 + api.php
  var appid     = 'chensen';    //后台WebAPI中设置的AppID
  var appsecret = 'chensen';    //后台WebAPI中设置的API认证密钥
  var timestamp = Date.parse(new Date());
  timestamp = timestamp / 1000;    //获取当前时间戳
  var signature =$.md5($.md5(appid + appsecret + timestamp))  //构建signature
  var data = {    //因为搜索、留言和表单需要用到POST方法，要通过data传递参数，所以先声明data对象，方便后面添加
    appid: appid,
    timestamp: timestamp,
    signature: signature,
  
  };

//根据请求方法构建Header
  if (method == 'POST') {
//POST方法，设置请求头部
    var header = { 'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8' };
//添加POST参数到data
    for (var i in parm) {
      data[i] = parm[i];
    }
  } else {
//GET方法，设置请求头部
    method = 'GET';
    var header = { 'Content-Type': 'application/json' };
  }

var result='';
	
if(formData !=1){
	$.ajax({
	//几个参数需要注意一下
	    type: method,//方法类型
	    dataType: "json",//预期服务器返回的数据类型
	    url: apiurl + url,//url
	     data: data,
		 async : false,
	     cache: false,        // 不缓存数据
	    success: function (data) {

/**
 * json  0失败  1成功  2跳转  3弹窗   4未激活  5冻结 6页面显示错误
 */
	    switch (data.code){
			case 0:
			 layer.open({
			   content: data.data
			   , skin: 'msg'
			   , time: 2 //2秒后自动关闭
			 });
			break ;
			case 1:
			 result = data;
			break ;
			case 2:
			layer.open({
			    content: data.data
			    , skin: 'msg'
			    , time: 2//2秒后自动关闭
			    , end: function () {
			        location.href = data.tourl;
			    }
			});
			break ;
			case 3:
		layer.open({
		    content: data.data.msg
		    , btn: '我知道了'
		});
		result = data;
			break ;
			case 4:
			layer.open({
			    content: '会员暂时未激活'
			    , btn: '我知道了'
			});
			break ;
			case 5:
		   layer.open({
		    content: '账号已经被冻结'
		    , skin: 'msg'
		    , time: 1//2秒后自动关闭
		    , end: function () {
		        location.href = data.tourl;
		    }
		});
			break ;
			case 6:
			 result = data;
				break ;
			default:
			     
		}
	    },
	 
	  });
}else{
	//formData上传数据
  var formData = new FormData();
		         formData.append('upload',data.upload);
				 formData.append('appid',appid);
				 formData.append('timestamp',timestamp);
				  formData.append('signature',signature);
				
$.ajax({
	//几个参数需要注意一下
	    type: method,//方法类型
	    dataType: "json",//预期服务器返回的数据类型
	    url: apiurl + url,//url
	    data: formData,
		async : false,
	     cache: false,        // 不缓存数据
	     header: header,
		  processData: false,
		  contentType: false,
	    success: function (data) {
	      if (data.code == 0) {
			  layer.open({
			    content: data.data
			    , skin: 'msg'
			    , time: 2 //2秒后自动关闭
			  });
			 
	      } else {
	       result = data;
	      }
	       if (data.code == 2) {
	           layer.open({
	               content: data.data
	               , skin: 'msg'
	               , time: 1//2秒后自动关闭
	               , end: function () {
	                   location.href = data.tourl;
	               }
	           });
	       }
	    },
	 
	  });
}

//返回数据

 return result;

}

$.fn.serializeObject = function() {
  var o = {};
  var a = this.serializeArray();
  $.each(a, function() {
    if (o[this.name]) {
      if (!o[this.name].push) {
        o[this.name] = [ o[this.name] ];
      }
      o[this.name].push(this.value || '');
    } else {
      o[this.name] = this.value || '';
    }
  });
  return o;
};

// slider

